package com.zzx.auth.conf;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * 配置数据源
 */
@Configuration
@Slf4j
@ConfigurationProperties(prefix = "order.jdbc")
@Data
public class DataSourceConfig {

    private String url;
    private String username;
    private String password;
    private Integer initialSize;
    private Integer maxActive;
    private Integer maxIdle;
    private Integer maxWait;
    private Integer minIdle;
    private String validationQuery;

    /**
     * 初始化数据源
     *
     * @return 数据源bean
     */
    @Bean(initMethod = "init")
    public DataSource getDataSource() {
        log.info("数据库初始化...");
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);// 用户名
        dataSource.setPassword(password);// 密码
        dataSource.setInitialSize(initialSize);
        dataSource.setMaxActive(maxActive);
        dataSource.setMaxWait(maxWait);
        dataSource.setMinIdle(minIdle);
        dataSource.setValidationQuery(validationQuery);
        return dataSource;
    }


}
